new Vue({
  el: "#app",
  data() {
    return {
      pfile: {},
      fileList: [],
      use: sessionStorage.getItem("User_Number"),
      form: {
        // Brand_SerialNumber: "",
        Brand_Name: "", //表名
        Picture_URL: "",
      },
      rules: {
        Brand_Name: [
          { required: true, message: "请输入品牌名称", trigger: "blur" },
        ],
      },
      loading: false,
    };
  },
  mounted() {
    // this.getID();
  },
  methods: {
    handleBeforeUpload(file) {
      // 生成预览URL并添加到fileList中
      const reader = new FileReader();
      reader.onload = (e) => {
        const newFile = {
          name: file.name,
          url: e.target.result, // 预览URL
          // 可以添加其他属性，如上传状态等
        };
        this.fileList.push(newFile);
      };
      reader.readAsDataURL(file);
      // 返回false以阻止自动上传
      return false;
    },
    handleChange(file, fileList) {
      // 这个方法现在主要用于调试或处理其他上传后的逻辑
      // 注意：这里的fileList参数与:file-list绑定的fileList不是同一个对象
      // :file-list绑定的fileList是组件内部维护的，而这里的fileList是上传事件提供的
      console.log("Upload change event:", file, fileList);
      fileList.push(file);
      this.pfile = file;
      // file.name = "未标题-2.png";
      // let dotIndex = file.name.lastIndexOf(".");
      // if (dotIndex !== -1) {
      //   let extension = file.name.substring(dotIndex);
      //   console.log(extension); // 输出: .png
      //   file.uid = file.uid + extension;
      // }
      console.log(file);
      console.log(fileList);
    },
    submitForm() {
      console.log(111, this.fileList, "this.fileList");
      // console.log(this.pfile);
      // let lArray = new Array();
      // lArray.push(this.pfile);
      const _this = this;
      // console.log(_this.pfile, "this.pfile");
      // let lArray = new Array();
      // lArray.push(_this.pfile);
      // return;
      // 存图片
      $.ajax({
        headers: {
          AccessToken: $.cookie("token"), //会将数据转换为query string
        },
        // headers: requestHeaders(), // 确保这个函数返回正确的头部信息
        url: "https://service.kdddy.cn/donghuiForRequest/store/photo",
        // url: "http://192.168.10.3:8083/article/articlePictureUpLoad",
        type: "POST", // 注意HTTP方法应该大写
        contentType: "application/json", // 设置请求体的媒体类型为application/json
        data: JSON.stringify(_this.fileList), // 将数据序列化为JSON字符串
        success: function (Result) {
          console.log(Result);
          _this.tj();
        },
        complete: function (XMLHttpRequest) {
          requestPretreatment(XMLHttpRequest); // 确保这个函数在config/config.js中定义
        },
        dataType: "json", // 预期服务器返回的数据类型
      });
    },
    // 查询企业信息和个人信息
    // tphtjdata() {
    //   console.log(this.use);

    //   const _this = this;

    //   $.ajax({
    //     headers: {
    //       AccessToken: $.cookie("token"), //会将数据转换为query string
    //     },
    //     // url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
    //     url: "https://service.kdddy.cn/donghuiForRequest/selectByData/selectEnterpriseID",
    //     type: "get",
    //     data: {
    //       Data: _this.use,
    //     },
    //     success: function (Result) {
    //       console.log(Result);
    //       _this.tj(Result);
    //     },
    //     complete: function (XMLHttpRequest) {
    //       requestPretreatment(XMLHttpRequest); //config/config.js 响应预检
    //     },
    //     dataType: "json",
    //   });

    //   // console.log(Enterprise_Info_SerialNumber);
    //   // this.tj(Enterprise_Info_SerialNumber);
    // },
    tj() {
      // console.log(res, "Enterprise_Info_SerialNumber");

      let Enterprise_Info_SerialNumber = sessionStorage.getItem(
        "Enterprise_Info_SerialNumber"
      );
      console.log(this.form);
      const _this = this;
      _this.form.Enterprise_Number = Enterprise_Info_SerialNumber;
      //   _this.form.Operation_ID = res.Data[0].Operation_ID;
      // let dotIndex = _this.fileList[0].name.lastIndexOf(".");
      // if (dotIndex !== -1) {
      //   let extension = _this.fileList[0].name.substring(dotIndex);
      //   console.log(extension); // 输出: .png
      //   _this.form.Picture_URL = _this.fileList[0].uid + extension;
      // }
      let base64Image = _this.fileList[0].url;
      let dataIndex = base64Image.indexOf("data:");
      let base64Index = base64Image.indexOf(";base64,");
      let substring = base64Image.substring(dataIndex + 5, base64Index);
      let ls_split = substring.split("/");
      _this.form.Picture_URL = _this.fileList[0].uid + "." + ls_split[1];
      console.log(_this.form, "v");

      let lArray = new Array();
      lArray.push(_this.form);
      let lJSONObject_business_brand = new Object();
      lJSONObject_business_brand.business_brand = lArray;
      let lJSONObject_DataSet = new Object();
      lJSONObject_DataSet.Add = lJSONObject_business_brand;

      console.log(lJSONObject_DataSet, "lJSONObject_DataSet");
      console.log(_this.form);

      console.log(_this.pfile, "this.pfile");

      // let dotIndex = file.name.lastIndexOf(".");
      // if (dotIndex !== -1) {
      //   let extension = file.name.substring(dotIndex);
      //   console.log(extension); // 输出: .png
      //   file.uid = file.uid + extension;
      // }
      // return;
      $.ajax({
        headers: {
          AccessToken: $.cookie("token"), //会将数据转换为query string
        },
        // headers: requestHeaders(),
        // url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
        url: "https://service.kdddy.cn/donghui/generalBusiness/generalSave",
        type: "POST",
        data: {
          Data: JSON.stringify(lJSONObject_DataSet),
        },
        success: function (Result) {
          console.log(Result);
          if (Result.Status === "success") {
            _this.$message.success("提交成功");
            window.history.go(-1);
          } else {
            _this.$message.error("提交失败，请稍后再试");
          }
        },
        complete: function (XMLHttpRequest) {
          requestPretreatment(XMLHttpRequest); //config/config.js 响应预检
        },
        dataType: "json",
      });
    },
    // submitForm(formName) {
    //   const that = this;
    //   this.$refs[formName].validate((valid) => {
    //     if (valid) {
    //       that.loading = true;
    //       $.ajax({
    //         headers: requestHeaders(),
    //         // url: `${BASE_URL2}/generalBusiness/generalSave`,
    //         url: "https://service.kdddy.cn/donghuiForRequest/selectByData/selectEnterpriseID",
    //         type: "POST",
    //         data: {
    //           Data: JSON.stringify({
    //             Add: {
    //               business_brand: [that.form],
    //               base_operation_log: [
    //                 generateLog(
    //                   "添加品牌数据(" + that.form.Brand_Name + ")",
    //                   "平台端操作",
    //                   "business_brand",
    //                   JSON.stringify({ Add: { business_brand: [that.form] } })
    //                 ),
    //               ],
    //             },
    //           }),
    //         },
    //         success: function (res) {
    //           if (res.Status === "success") {
    //             that.$message.success("提交成功");
    //             window.history.go(-1);
    //           } else {
    //             that.$message.error("提交失败，请稍后再试");
    //           }
    //         },
    //         error: function () {
    //           that.$message.error("操作失败，请稍后再试");
    //         },
    //         complete: function (XMLHttpRequest) {
    //           that.loading = false;
    //           requestPretreatment(XMLHttpRequest);
    //         },
    //         dataType: "json",
    //       });
    //     } else {
    //       that.$message({
    //         message: sumberError,
    //         type: "warning",
    //       });
    //       return false;
    //     }
    //   });
    // },
    // resetForm(formName) {
    //   this.$refs[formName].resetFields();
    // },
  },
});
